﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ControlAccessUser.Model;
using ControlAccessUser.Bussiness;

namespace ControlAccessUser.Presenter
{
    public class PresenterLogin
    {
        ILoginView View;
        
        public PresenterLogin(ILoginView View)
        {
            this.View = View;
        }

        public void CheckLogin()
        {
            try
            {
                User UserToVerify = new User() { UserName = View.UserNameLogin, UserPassword = View.UserPasswordLogin};
                View.UserVerify = ManagerUser.GetUserByUserNameAndUserPassword(UserToVerify);
                View.GoPrincipalWindow(View.UserVerify);
            }
            catch (Exception ex)
            {
                View.ErrorMessage(ex.Message);
            }
        }

        public void ValidateLogin()
        {
            User user = new User() {UserName=View.UserNameLogin};
            try
            {
                View.UserNameLogin = ManagerUser.ValidateUserName(user);
                View.UserNameLoginChanged = View.UserNameLogin;
            }
            catch (Exception)
            {
                View.UserNameLogin = View.UserNameLoginChanged;
            }
        }

        public void RegisterEntryOrExit()
        {
            try
            {
                User UserToVerify = new User() { UserName = View.UserNameLogin, UserPassword = View.UserPasswordLogin };
                View.SystemMessage = ManagerUser.RegisterEntryOrExitUser(UserToVerify);
            }
            catch (Exception ex)
            {
                View.ErrorMessage(ex.Message);
            }
        }
    }
}
